﻿using DoNet.Bpmn.Core.Enums;
using DoNet.Bpmn.Core.Utils;
using DoNet.Bpmn.Core.Vo;
using Microsoft.Extensions.Logging;

namespace DoNet.Bpmn.Adaptor.BpmnElement
{
    public class BpmnElementOutSideAccessAdaptor : BpmnElementAdaptor
    {
        private readonly ILogger<BpmnElementOutSideAccessAdaptor> _logger;

        public BpmnElementOutSideAccessAdaptor(ILogger<BpmnElementOutSideAccessAdaptor> logger)
        {
            _logger = logger;
        }

        protected override BpmnConfCommonElementVo? GetElementVo(BpmnNodePropertysVo? property, BpmnNodeParamsVo? paramsVo, int elementCode, string elementId)
        {
            string collectionName = "outSideAccessList";
            int? signType = null;
            if (property != null)
            {
                signType = property.SignType;
            }

            if (paramsVo != null)
            {
                // Handle third party element approval assignees
                var assigneeMap = paramsVo.AssigneeList.ToDictionary(
                    bpmnNodeParamsAssigneeVo => bpmnNodeParamsAssigneeVo.Assignee,
                    bpmnNodeParamsAssigneeVo => bpmnNodeParamsAssigneeVo.AssigneeName
                );

                var elementName = paramsVo.AssigneeList.FirstOrDefault()?.ElementName;
                if (!string.IsNullOrEmpty(elementName))
                {
                    if (signType == (int)SignTypeEnum.SIGN_TYPE_SIGN)
                    {
                        return BpmnElementUtils.GetMultiplayerSignElement(
                            elementId,
                            elementName,
                            $"{collectionName}{elementCode}",
                            assigneeMap.Keys.ToList(),
                            assigneeMap
                        );
                    }
                    else
                    {
                        return BpmnElementUtils.GetMultiplayerOrSignElement(
                            elementId,
                            elementName,
                            $"{collectionName}{elementCode}",
                            assigneeMap.Keys.ToList(),
                            assigneeMap
                        );
                    }
                }
            }
            return default;
        }

        public override void SetSupportBusinessObjects()
        {
            ((IAdaptorService)this).AddSupportBusinessObjects(NodePropertyEnum.NODE_PROPERTY_OUT_SIDE_ACCESS);
        }
    }
}
